package com.kelvin.paopao.module.market.market.EditRecieveLocation;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyphenate.easeui.utils.SpUtil;
import com.kelvin.kelvinlib.utils.CommonUtil;
import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.adapter.EditLocationAdapter;
import com.kelvin.paopao.injector.components.DaggerEditLocationComponent;
import com.kelvin.paopao.injector.modules.EditLocationModule;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.utils.Constant;
import com.kelvin.paopao.widgets.CustomToolbar;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 编辑收货地址的activity
 * Created by Administrator on 2017/5/16.
 */

public class EditRecieveLocationActivity extends BaseActivity<EditLocationPresenter> {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_area)
    EditText etArea;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private SuggestionSearch mSuggestionSearch;
    private LinearLayoutManager manager;
    private List<SuggestionResult.SuggestionInfo> beans;

    @Inject
    EditLocationAdapter adapter;

    private double latitude, longtitude;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_edit_location;
    }

    @Override
    protected void initInjector() {
        DaggerEditLocationComponent.builder().editLocationModule(new EditLocationModule(this))
                .build().inject(this);

    }

    @Override
    protected void initViews() {
        customToolbar.showBackIcon(this);

        //初始化百度在线建议
        mSuggestionSearch = SuggestionSearch.newInstance();
        OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
            public void onGetSuggestionResult(SuggestionResult res) {
                if (res == null || res.getAllSuggestions() == null) {
                    return;
                    //未找到相关结果
                }
                //获取在线建议检索结果
                beans = res.getAllSuggestions();
                adapter.setNewData(dropNullLng(beans));
            }
        };
        mSuggestionSearch.setOnGetSuggestionResultListener(listener);

        manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                etArea.setText(((SuggestionResult.SuggestionInfo)
                        adapter.getData().get(position)).key);
                latitude = ((SuggestionResult.SuggestionInfo)
                        adapter.getData().get(position)).pt.latitude;
                longtitude = ((SuggestionResult.SuggestionInfo)
                        adapter.getData().get(position)).pt.longitude;

            }
        });
        recyclerView.setAdapter(adapter);

        etArea.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s)) {
                    //展示全部记录
                    doSearchByBD(s.toString());
                }
            }
        });
    }

    /**
     * 用百度在线搜索来做查询
     */
    private void doSearchByBD(String tag) {
        mSuggestionSearch.requestSuggestion((new SuggestionSearchOption())
                .keyword(tag)
                .city(Constant.city));
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick(R.id.tv_add)
    public void onClick() {
        String username = etName.getText().toString().trim();
        String phone = etPhone.getText().toString().trim();
        String area = etArea.getText().toString().trim();
        String address = etAddress.getText().toString().trim();

        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(phone) || TextUtils.isEmpty(area)
                || latitude == 0 || longtitude == 0) {
            ToastUtil.showToast(this, "请填写完整再上传");
            return;
        }
        if (!CommonUtil.isPhoneNo(phone)) {
            ToastUtil.showToast(this, "手机号码格式不正确");
            return;
        }

        mPresenter.addLocation(username, phone, address, area, String.valueOf(latitude)
                , String.valueOf(longtitude));
    }

    public static void launch(Context context) {
        Intent intent = new Intent(context, EditRecieveLocationActivity.class);
        context.startActivity(intent);
    }

    /**
     * 删除掉没有经纬度的地址
     */
    private List<SuggestionResult.SuggestionInfo>
    dropNullLng(List<SuggestionResult.SuggestionInfo> infos) {
        List<SuggestionResult.SuggestionInfo> newInfos = new ArrayList<>();
        for (SuggestionResult.SuggestionInfo info : infos) {
            if (info.pt != null && info.pt.latitude != 0 && info.pt.longitude != 0) {
                newInfos.add(info);
            }
        }
        return newInfos;
    }
}
